package com.auerdi.ui.fragment.b_classify;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.base.BaseMainFragment;
import com.auerdi.event.TabSelectedEvent;
import com.auerdi.ui.fragment.b_classify.child.ADClaHomeFragMent;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.CategoryList;
import com.example.egghttp.retrofit.beans.goods.FCategory;
import com.example.egghttp.retrofit.beans.goods.F_Category;
import com.example.egghttp.retrofit.beans.goods.SecondCategory;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/6/30.
 */
public class AdClassifyTabFragment extends BaseMainFragment {

    Unbinder unbinder;
    @BindView(R.id.left_list)
    RecyclerView leftList;
    @BindView(R.id.right_list)
    RecyclerView rightList;
    private List<SecondCategory> categorieList = new ArrayList<>();

    private GridLayoutManager outLayoutmager;
    private int selectLestPosition = 0;
    private List<FCategory> catList = new ArrayList<>();

    public static AdClassifyTabFragment newInstance() {

        Bundle args = new Bundle();

        AdClassifyTabFragment fragment = new AdClassifyTabFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_tab_classify, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBusActivityScope.getDefault(_mActivity).register(this);


        outLayoutmager = new GridLayoutManager(getActivity(), 1);
        rightList.setLayoutManager(outLayoutmager);

        rightList.setAdapter(new CommonAdapter<FCategory>(getActivity(), R.layout.item_classify_right_sub, catList) {
            @Override
            protected void convert(ViewHolder viewHolder, final FCategory outitem, int position) {

                viewHolder.setText(R.id.title, "" + outitem.getTitle());

                RecyclerView recyclerView = viewHolder.getView(R.id.left_list);
                recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                recyclerView.setAdapter(new CommonAdapter<F_Category>(recyclerView.getContext(), R.layout.item_classify_right_thaid, outitem.getF_category()) {
                    @Override
                    protected void convert(ViewHolder viewHolder, final F_Category item, int position) {

                        viewHolder.setText(R.id.title, "" + item.getTitle());
                        ImageView imageView = viewHolder.getView(R.id.img);
                        Picasso.get().load(item.getImg()).error(R.mipmap.epta).placeholder(R.mipmap.epta).fit().into(imageView);
                        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                EventBus.getDefault().post(ADClaHomeFragMent.newInstance(item.getId(), item.getTitle()));
                            }
                        });
                    }
                });

            }
        });

        leftList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        leftList.setAdapter(new CommonAdapter<SecondCategory>(getActivity(), R.layout.item_classify_left, categorieList) {
            @Override
            protected void convert(ViewHolder viewHolder, final SecondCategory item, int position) {
                viewHolder.setText(R.id.title, item.getTitle());
                TextView title = viewHolder.getView(R.id.title);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    title.setBackground(getResources().getDrawable(position == selectLestPosition ? R.drawable.login_bg_v3 : R.drawable.bg_transpant));//背景色设为灰色
                } else {
                    title.setBackgroundDrawable(getResources().getDrawable(position == selectLestPosition ? R.drawable.login_bg_v3 : R.drawable.bg_transpant));//背景色设为灰色
                }
                title.setTextColor(getResources().getColor(position == selectLestPosition ? R.color.white : R.color.black));//背景色设为灰色
                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        selectLestPosition = position;
                        List<FCategory> tmp = categorieList.get(selectLestPosition).getProLists();

                        catList.clear();
                        catList.addAll(tmp);
                        rightList.getAdapter().notifyDataSetChanged();
                        notifyDataSetChanged();

                    }
                });


            }
        });


        RetrofitClient.getClient().create(AdService.class).categoryCateList()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<CategoryList>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BOb<CategoryList> bannerListBOb) {

                        List<SecondCategory> ddd = bannerListBOb.getBody().getProLists();
                        List<FCategory> tmp = ddd.get(selectLestPosition).getProLists();


                        categorieList.clear();
                        categorieList.addAll(ddd);

                        catList.clear();
                        catList.addAll(tmp);


                        leftList.getAdapter().notifyDataSetChanged();
                        rightList.getAdapter().notifyDataSetChanged();

//                       L.d("hcia"," bannerListBOb.getBody().getProLists().size():"+ bannerListBOb.getBody().getProLists().size());


                    }
                });


        return view;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }


    /**
     * Reselected Tab
     */
    @Subscribe
    public void onTabSelectedEvent(TabSelectedEvent event) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
        unbinder.unbind();
    }


}
